<template>
  <div class="w">

    <!-- 标题 -->
    <div class="title-area">
      <span>
        评测任务管理
      </span>
    </div>

    <!-- 主体内容显示区 -->
    <div class="content-area">

      <!-- 查询表单区 -->
      <div class="form-area">
        <Form
          name="form"
          ref="formRef"
          :labelCol="{span: 7, offset: 0}"
          @finish="formFinish"
          :initialValues="queryForm">

          <Row>
            <Col :span="6">
              <FormItem name="reviewTaskNumber" label="任务编号">
                <Input placeholder="请输入任务编号" style="width: 70%" />
              </FormItem>
            </Col>
            <Col :span="6">
              <FormItem name="reviewTaskName" label="任务名称">
                <Input placeholder="请输入任务名称" style="width: 70%" />
              </FormItem>
            </Col>
            <Col :span="6">
              <FormItem name="startTime" label="开始日期" style="position: relative;" class="selectPosition">
                <DatePicker placeholder="请选择日期" mode="date" style="width: 70%" :getPopupContainer="getPopupContainer"/>
              </FormItem>
            </Col>
            <Col :span="6">
              <FormItem :wrapperCol="{span: 20, offset: 5}">
                <Button type="primary" @click="formQuery">
                  <Icon name="search" style="margin-top: 3px; margin-right: 5px;" /> 查 询
                </Button>
                <Button style="margin-left: 10px" @click="formReset">
                  <Icon name="rollback-outline" style="margin-top: 3px; margin-right: 5px;" /> 重 置
                </Button>
              </FormItem>
            </Col>
          </Row>  
        </Form>
      </div>

      <!-- 分割线 -->
      <Divider />

      <!-- 按钮区 -->
      <div class="button-area">
        <Button type="primary" @click="tblOptForm()">
          <Icon name="add" /> 新增
        </Button>
      </div>

      <!-- 列表区 -->
      <Skeleton
        style="margin-top: 15px;"
        :loading="isDataLoading"
        :avatar="false"
        :paragraph="{ rows: 15 }"
        active>

        <Table
          :columns="tableColumns"
          :dataSource="tableData"
          :bordered="false"
          :rowKey="(record) => record.key"
          :scroll="{ y: 470 }"
          @change="handleTableChange"
          :pagination="{
            total: total,
            showQuickJumper: true,
            showSizeChanger: true,
            pageSize: queryForm.pageSize,
            current: queryForm.pageNum,
          }">
          <template #render="render">
            <div v-if="render.dataIndex == 'produceId'">
              <span>{{ produceInfo(render.record.produceId) }}</span>
            </div>
            <div v-else-if="render.dataIndex == 'operation'">
              <a href="#" @click.prevent="tblOptForm(render.record)">
                <Icon name="edit" style="margin-top: 4px;" /> 编辑
              </a>
              <a href="#" @click.prevent="removeTask(render.record)">
                <Icon name="delete-can" style="margin-top: 4px;" /> 删除
              </a>
            </div>
          </template>
          <template #paginationTotal="props">
            <span>总共 {{ props.total }} 条</span>
          </template>
        </Table>
      </Skeleton>
    </div>

    <!-- 表单页对话框 -->
    <Modal
      class="fromModal"
      :title="formDialogInfo.title"
      v-model="formDialogInfo.visible"
      @cancel="formDialogInfo.visible = false"
      width="1100px"
      :bodyStyle="{ padding: '0px' }"
      :footer="[null]">

      <task-form
        :key="Date.now()" 
        :reviewTaskId="formDialogInfo.reviewTaskId" 
        :currRow="formDialogInfo.currRow" 
        :refData="{
          firmsList: firmsList,
          producesList: producesList,
          reviewCaseIdList: reviewCaseIdList,
          reviewCaseList: reviewCaseList,
          taskDetail: taskDetail
        }" 
        @handleCancel="formDialogCancel" />
    </Modal>
  </div>
</template>

<script>
import { Message } from '@uni/vue';
import { listTask, getTask, delTask } from "@/api/doreview/reviewtask";
import taskForm from "./form.vue"

export default {
  name: "app",
  components: {
    taskForm
  },
  data: function () {
    return {
      firmsList: [],
      producesList: [],
      reviewCaseIdList: [],
      reviewCaseList: [],
      formDialogInfo: {
        title: '',
        visible: false,
        reviewTaskId: 0,
        currRow: {}
      },
      taskDetail: {},
      isDataLoading: false,
      queryForm: {
        reviewTaskNumber: '',
        reviewTaskName: '',
        startTime: undefined
      },
      startTime: {},
      tableColumns: [
        // {"title":"序号", "dataIndex":"reviewTaskId", "key":"reviewTaskId", "align":"center", width: 50},
        {"title":"任务编号", "dataIndex":"reviewTaskNumber", "key":"reviewTaskNumber", "align":"center", width: 150},
        {"title":"任务名称", "dataIndex":"reviewTaskName", "key":"reviewTaskName", "align":"center"},
        {"title":"针对产品", "dataIndex":"produceId", "key":"produceId", "align":"center"},
        {"title":"开始日期", "dataIndex":"startTime", "key":"startTime", "align":"center", "width":100},
        {"title":"任务描述", "dataIndex":"remark", "key":"remark", "align":"center"},
        {"title":"操作", "dataIndex":"operation", "key":"operation", "align":"center", "width":120},
      ],
      tableData: [],
      total: 0
    }
  },
  mounted: async function () {
    await this.queryTaskDetail();
    await this.formFinish();
  },
  computed: {
    produceInfo: function () {
      return (val) => {
        const produce =  this.producesList.find(produce => produce.produceId == val);
        return produce ? `${produce.firm.firmName} -> ${produce.produceName}` : '<未知>';
      }
    }
  },
  methods: {
    getPopupContainer() {
      return document.querySelector(".selectPosition")
    },
    queryTaskDetail: async function (id) {
      const rs = await getTask(id);
      if (rs.code !== 200) return Message.error({ text: '获取评测用例明细失败：' + rs.msg });

      // console.log("queryTaskDetail: ", rs);
      this.taskDetail = rs.data ? rs.data : {};
      this.firmsList = rs.firms;
      this.producesList = rs.produces;
      this.reviewCaseIdList = rs.reviewCaseIds ? rs.reviewCaseIds : [];
      this.reviewCaseList = rs.reviewCases;
    },
    formQuery: function () {
      this.$refs.formRef.submit();
    },
    formReset: function () {
      this.$refs.formRef.reset();
    },
    queryList: async function () {
      this.isDataLoading = true;
      const rs = await listTask(this.queryForm);
      if (rs.code !== 200) return Message.error({ text: '获取评测任务列表信息失败：' + rs.msg });
      this.tableData = rs.rows;
      this.total = rs.total;
      // console.log("rs: ", rs);
      this.isDataLoading = false;
    },
    formFinish: async function (formValues) {
      this.queryForm = {
        pageNum: 1,
        pageSize: 10,
        reviewTaskNumber: formValues ? formValues.reviewTaskNumber : '',
        reviewTaskName: formValues ? formValues.reviewTaskName : '',
        startTime: (formValues && formValues.startTime)  ? formValues.startTime.format('YYYY-MM-DD')  : ''
      };
      this.queryList();
    },
    handleTableChange: function (p, filters) {
      const { current, pageSize } = p;
      this.queryForm.pageNum = current;
      this.queryForm.pageSize = pageSize;
      this.queryList();
      console.log('filters', p, filters);
    },
    tblOptForm: async function (record) {
      this.formDialogInfo.reviewTaskId = record ? record.reviewTaskId : 0;
      this.formDialogInfo.currRow = record ? record : {};
      this.formDialogInfo.title = (record ? '修改' : '新增') + '评测任务信息';

      await this.queryTaskDetail(record ? record.reviewTaskId : null);
      this.formDialogInfo.visible = true;
    },
    formDialogCancel: function () {
      this.formDialogInfo.visible = false;
      this.formFinish();
    },
    removeTask: function (record) {
      this.$MessageBox.ModalConfirm(`是否确认删除名称为 "${record.reviewTaskName}" 的数据项？`, async () => {
        const rs = await delTask(record.reviewTaskId);
        if (rs.code !== 200) return Message.error({ text: '删除评测任务失败：' + rs.msg });
        this.formQuery();
        Message.success({ text: "删除评测任务信息完成" });
      });
    }
  }
}
</script>

<style scoped lang="scss">
  .w {
    /* height: 100%; */
    background-color: #F9FAFB;
  }

  /* 标题 */
  .title-area {
    height: 36px;
    background-color: #E9EBF0;
  }
  .title-area span {
    display: inline-block;
    font-weight: 500;
    line-height: 36px;
    margin-left: 15px;
  }

  /* 主体内容显示区 */
  .content-area {
    /* background-color: lightblue; */
    height: 100%;
    margin-left: 25px;
    margin-right: 25px;
    padding-bottom: 15px;
  }

  /* 查询表单区 */
  .form-area {
    /* background-color: lightcyan; */
    margin-top: 15px;
    margin-left: 50px;
    margin-right: 50px;
  }

  .uni-divider {
    margin: 0;
  }

  /* 按钮区 */
  ::v-deep .button-area {
    margin: 20px 0 8px;
     .uni-btn-inner {
      display: flex;
      align-items: center;
    }
    /* background-color: lightcyan; */
  }
  .button-area svg {
    margin-right: 5px;
  }


  /* 列表区 */

  /* 弹框区 */
  ::v-deep .fromModal > .uni-modal-wrap > .uni-modal-wrap-content > .uni-modal-footer {
    padding: 0;
  }
</style>

